<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="用户名称">
        <el-input v-model="teacher.username" />
      </el-form-item>
      <el-form-item label="用户排序">
        <el-input-number v-model="teacher.sort" controls-position="right" min="0" />
      </el-form-item>
      <el-form-item label="用户类型">
        <el-select v-model="teacher.type" clearable placeholder="请选择">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
          -->
          <el-option :value="'admin'" label="管理员" />
          <el-option :value="'user'" label="学生" />
        </el-select>
      </el-form-item>

       <el-form-item label="性别">
        <el-select v-model="teacher.sex" clearable placeholder="请选择">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
          -->
          <el-option :value="'0'" label="男" />
          <el-option :value="'1'" label="女" />
        </el-select>
      </el-form-item>

      <el-form-item label="用户简介">
        <el-input v-model="teacher.remarks" :rows="10" type="textarea" />
      </el-form-item>

      <!-- 讲师头像：TODO -->
      <el-form-item label="用户头像">
        <!-- 头衔缩略图 -->
        <pan-thumb :image="teacher.userImg" />
        <!-- 文件上传按钮 -->
        <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像</el-button>

        <!--
            v-show：是否显示上传组件
            :key：类似于id，如果一个页面多个图片上传控件，可以做区分
            :url：后台上传的url地址
            @close：关闭上传组件
        @crop-upload-success：上传成功后的回调-->
        <image-cropper
          v-show="imagecropperShow"
          :width="300"
          :height="300"
          :key="imagecropperKey"
          :url="BASE_API+'/eduService/oss/upload'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"
        />
        
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";
import eduUser from "@/api/eduUser";
// const defaultForm = {
//   userImg: "https://inq2019.oss-cn-shenzhen.aliyuncs.com/userIng/moren.png",
 
// };
export default {
  components:{
    ImageCropper, PanThumb
  },
  data() {
    return {
      BASE_API: process.env.BASE_API,
       imagecropperShow: false, // 是否显示上传组件
      imagecropperKey: 0 ,// 上传组件id
      teacher: {
        userImg: "https://inq2019.oss-cn-shenzhen.aliyuncs.com/userIng/moren.png",
        username:"",
        type:"user",
        remarks:"",
        sort:"",
        sex:"0"

      },
      saveBtnDisabled: false // 保存按钮是否禁用,
    };
  },
  //页面渲染之前调用
  created() {
    this.init();
  },
  //监听路由:保存时数据回显问题
  watch: {
    $route(to, from) {
      //   console.log("watch $route");
      this.init();
    }
  },
  //具体方法调用
  methods: {
    init() {
      if (this.$route.params && this.$route.params.id) {

        const id = this.$route.params.id;
        this.uptTeacher(id);
      } else {
        this.teacher = { ...defaultForm };
      }
    },

    saveOrUpdate() {

        this.saveTeacher();
    
    },
    /**
     * 保存
     */
    saveTeacher() {
      // console.log(this.teacher)
      eduUser
        .usave(this.teacher)
        .then(response => {
          return this.$message({
            type: "success",
            message: "保存成功!"
          });
        })
        .then(resposne => {
          this.$router.push({ path: "/stuteacher/addUser/" });
        })
        .catch(response => {
          // console.log(response)
          this.$message({
            type: "error",
            message: "保存失败"
          });
        });
    },
    /**
     * 更新
     */
    uptTeacher(id) {
      eduUser
        .getbyid(id)
        .then(response => {
          this.teacher = response.data.user;
        })
        .catch(response => {
          this.$message({
            type: "error",
            message: "获取数据失败"
          });
        });
    },
    defaultForm(){

    },
    //图片相关
    cropSuccess(data) {
    this.imagecropperShow = false
    this.teacher.avatar=data.imgurl
    this.imagecropperKey = this.imagecropperKey + 1
    },
    min(){

    }
    ,

    // 关闭上传组件
    close() {
       this.imagecropperShow = false
      // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1
    }
  }
};
</script>